package com.lagou.module4_6;

/**
 * @author wsl
 * @date 2021/8/1 20:53
 * @project_name javase
 */
public class SubRunnable implements Runnable {
    private static int num = 100;//用于记录总票数

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (num > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName()+"买票，票号是：" + num);
                    num--;//出售一张票，则减一
                } else {
                    break;
                }
            }
        }
    }

    public static void main(String[] args) {

        SubRunnable sr = new SubRunnable();

        Thread t1 = new Thread(sr,"窗口一");
        t1.start();

        Thread t2 = new Thread(sr,"窗口二");
        t2.start();

        Thread t3 = new Thread(sr,"窗口三");
        t3.start();

    }
}
